package behavioralPattern.memento.example02;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 玩家类
 *  -- 发起人角色
 */
@Data
public class Player {
    private static String[] fruitPool = {"🍑","🍌","🍇","🍎"};

    private int money;
    private List<String> fruits = new ArrayList<>();

    public Player(int money) {
        this.money = money;
    }

    Random random = new Random();

    // 获取当前所有金币
    public int getMoney(){
        return this.money;
    }

    // 获取一个水果
    public String getFruit(){
        String prefix = "";

        if (random.nextBoolean()){
            prefix = "好吃的";
        }

        // 从数组中拿一个水果
        String f = fruitPool[random.nextInt(fruitPool.length)];
        return prefix + f;
    }

    // 掷骰子
    public void yacht(){
        int dice = random.nextInt(6) + 1;

        if (dice == 1){
            money++;
            System.out.println("金币 +1 ");
        } else if (dice == 2){
            money--;
            System.out.println("金币 -1 ");
        } else if (dice == 6){
            String fruit = getFruit();
            fruits.add(fruit);
            System.out.println("得到水果：" + fruit);
        } else {
            System.out.println("啥也没有");
        }
    }

    // 创建备忘录 - 创建快照
    public Memento createMemento(){
        Memento memento = new Memento(money);
        for (String fruit : fruits) {
            // 判断：只保存 好吃的
            if (fruit.startsWith("好吃的")){
                memento.addFruit(fruit);
            }
        }
        return memento;
    }

    // 撤销方法
    public void restoreMemento(Memento memento){
        this.money = memento.getMoney();
        this.fruits = memento.getFruits();
    }
}
